<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8" />
    <title>详情</title>
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
    <link href="../../../../css/mui.min.css" rel="stylesheet" />
    <link href="../../css/common.css" rel="stylesheet" />
    <link href="../../css/helper.css" rel="stylesheet" />
    <style>
      .mui-content {
        padding-top: 61px !important;
        padding-left: 13px;
        padding-right: 13px;
        margin-bottom: 15px;
      }
      
      .mui-content p {
        text-align: justify;
      }
      
      .file-pre-box {
        height: 443px;
        background: #f5f6f7;
        border: 1px solid #034880;
      }
      
      .file-pre {
        height: 443px;
        padding-left: 20px;
        padding-right: 20px;
        background: #f5f6f7;
        border: 1px solid #034880;
      }
      
      .img-tool-box {
        height: inherit;
        min-width: 33px;
      }
      
      .img-tool {
        width: 19px;
        height: 19px;
        margin-top: 10px;
      }
    </style>
  </head>

  <body>
    <div id="container" v-cloak>
      <header class="mui-bar mui-bar-nav">
        <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
        <h1 class="mui-title">详情</h1>
        <a class="mui-pull-right mui-bar-right" v-if="!store">
          <span @click="addStore()">
            <img src="../../img/shoucang_yishoucang.png" class="img-tool" style="margin-left: 13px" v-if="detils.isCollection"/>
            <img src="../../img/shoucang_moren.png" class="img-tool" style="margin-left: 13px" v-else/>
          </span>
          <span @click="downlonds()">
          	<img src="../../img/xiazai.png" class="img-tool" style="margin-left: 13px" v-if="isDown"/>
          	<img src="../../img/xiazai1.png" class="img-tool" style="margin-left: 13px" v-else/>
          </span>
        </a>
      </header>
      <div class="mui-content">
        <p>
          描述：
          <div>
          	{{detils.fileDescribe}}
          </div> 
        </p>
        <p>
          文件：
          <div @tap="prever()" class="link mui-btn-link">
            {{detils.title}}
          </div>
        </p>
        <!--<div class="file-pre">
          <h4 style="text-align: center">{{detils.title}}</h4>
          <div class="fileimg">
            <img :src="baseUrl + detils.fileAddres" alt="" />
          </div>
        </div>-->
      </div>
    </div>
    <script type="text/javascript" src="../../../../js/mui.js"></script>
    <script type="text/javascript" src="../../../../js/jquery-3.2.1.min.js"></script>
    <script src="../../../../js/vue.js"></script>
    <script src="../../js/mkeyplus.base.js"></script>
    <script src="../../js/sqlite.js"></script>
    <script type="text/javascript">
      mui.init()
      var ids = 1
      if(window.plus) {
        plusready()
      } else {
        document.addEventListener("plusready", plusready, false)
      }

      function plusready() {
        var self = plus.webview.currentWebview()
        console.log(self.ids)
        ids = self.ids
        vm.ids = self.ids
        if(!self.store) {
          vm.getDetils(vm.ids)
          vm.store = false 
        }
        let tech = [{
          key: "id",
          value: vm.ids,
        }]
        mui.DB.selectTableData("dataDevice", tech)
          .then((res) => {
            if(res.length) {
              vm.isDown = true
              if(self.store) {
                vm.detils = res[0]
                vm.fileStorePath = res[0].fileAddres
              }
            } else {
              vm.isDown = false
            }
            console.log(vm.isDown)
          })
          .catch((error) => {
            console.log(error)
          })
      }
      let vm = new Vue({
        el: "#container",
        data: {
          form: {
            page: 1,
            size: 20,
            system: "APP",
            status: 1,
          },
          ids: "",
          detils: {},
          baseUrl: mui.baseApp.serverUrl,
          fileStorePath: '',
          isDown: false,
          store: true //是否从下载进入
        },
        created: function() {
          // this.getDetils()
        },
        mounted() {
          // this.getDetils(this.ids)
        },
        methods: {
          getDetils(ids) {
            let than = this
            mui.baseApp.httpRequst(
              "get",
              "/app/api/knowledge/equipment/detail/" + ids, {},
              function(data) {
                console.log("12", data.data)
                than.detils = data.data
                // plus.nativeUI.closeWaiting();
              },
              function() {
                // plus.nativeUI.closeWaiting()
                console.log("获取数据失败1")
              }
            )
          },
          prever() {
            console.log(this.detils)
            if(!this.detils.fileId) {
              mui.toast('暂无文件')
              return
            }
            let that = this
            let filePath = 'knowledge/device/' + this.detils.fileId + '/' + this.detils.title
            if(this.fileStorePath) {
              plus.runtime.openFile(this.fileStorePath, {}, function (e) {
                console.log("打开失败", e) 
              })
            } else {
              plus.nativeUI.showWaiting("打开中")
              mui.baseApp.download(
                this.baseUrl + this.detils.fileAddres, 
                filePath,
                function (res) {
                  plus.nativeUI.closeWaiting()
                  console.log(res)
                  that.fileStorePath = res.filename
                  plus.runtime.openFile(res.filename, {}, function (e) {
                    console.log("打开失败", e)
                  })
                },
                function () {
                  plus.nativeUI.closeWaiting()
                }
              )
            }
            
          },
          async downlonds() {
            plus.nativeUI.showWaiting('下载中');
            let itms = this.detils
            let parm = {
              id: itms.id,
              areaId: itms.areaId || "",
              areaName: itms.areaName || "",
              deviceModelId: itms.deviceModelId || "",
              deviceModelIds: itms.deviceModelIds || "",
              deviceModelName: itms.deviceModelName || "",
              machineModel: itms.machineModel || "",
              functionalLocation: itms.functionalLocation || "",
              title: itms.title || "",
              fileId: itms.fileId || "",
              fileAddres: itms.fileAddres || "",
              createId: itms.createId || "",
              createName: itms.createName || "",
              createTime: itms.createTime || "",
              deleted: itms.deleted || "",
              isCollection: itms.isCollection || "",
              dm: itms.deviceModelName || "",
              t: itms.machineModel || "",
              fn: itms.title || "",
              cn: itms.createName || "",
            }
            if(!itms.fileId) {
              this.downCall(parm)
              return 
            }
            let filePath = 'knowledge/device/' + itms.fileId + '/' + itms.title;
            await mui.baseApp.download(
              vm.baseUrl + itms.fileAddres, 
              filePath,
              function (res) {
                console.log(res)
                parm.fileAddres = res.filename
                vm.fileStorePath = res.filename
              },
              function () {
                plus.nativeUI.closeWaiting();
              }
            );
            this.downCall(parm)
          },
          downCall(parm) {
            mui.DB.createFormat("dataDevice", parm, "id")
            mui.DB.insertOrReplace("dataDevice", parm)
            mui.fire(plus.webview.currentWebview().opener(), 'collection', {id: this.detils.id, isDown: true})
            vm.isDown = true
            plus.nativeUI.closeWaiting();
            mui.toast("操作成功");
          },
          addStore() {
            console.log("2222222")
            mui.baseApp.httpRequst(
              "post",
              "/app/api/knowledge/collection/add",
              {
                tagetId: this.detils.id,
                type: 2,
              },
              (data) => {
                this.detils.isCollection = !this.detils.isCollection
                mui.fire(plus.webview.currentWebview().opener(), 'collection', {id: this.detils.id, isCollection: this.detils.isCollection})
                mui.toast("操作成功")
              }
            )
          },
        },
      })
    </script>
  </body>

</html>